"""
演示小球不放回问题

"""
import random

# 定义一个列表,表示袋中的球
balls = ['W', 'W', 'W', 'W', 'R', 'R']


# 定义一个函数,用于模拟放回抽样
def simulate_with_not_replacement(balls,num_samplings):
    # 定义变量,用于记录白球和红球的数量
    white_count = 0
    red_count = 0
    both_white_count = 0
    both_red_count = 0
    # 循环模拟放回抽样
    for i in range(num_samplings):
        balls_2 = balls[:]
        # 第一次抽取
        ball1 = random.choice(balls_2)
        if ball1 == 'W':
            white_count += 1
            balls_2.remove(ball1)
        elif ball1 == 'R':
            red_count += 1
        balls_2.remove(ball1)
        # 进行第二次抽取
        ball2 = random.choice(balls_2)
        if ball2 == ball1:
            if ball1 == 'W':
                both_white_count += 1
            elif ball1 == 'R':
                both_red_count += 1

        # 返回白球数量,红球数量,白球和红球数量同时出现的数量
    return white_count, red_count, both_white_count, both_red_count

# 调用函数,模拟取球过程
white_count, red_count, both_white_count, both_red_count = simulate_with_not_replacement(balls=balls,num_samplings=1000)

# 输出结果
print("取到两个都是白球的概率为:", both_white_count / 1000)
print("取到两个都是红球的概率为:", both_red_count / 1000)







